/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	深圳市国省检日常化监管系统         </p>
 * <p>创建者   :	win7 
 * 
 * <p>描   述  :   FourTechnologyServiceImpl.java for com.pengtu.gsj.service.impl.check.jsbz.newTechnology    </p>
 * 
 * <p>最后修改 : $: 2017年8月11日-上午11:57:26 v 1.0.0	 win7   $ </p>
 * 
*/

package com.pengtu.gsj.service.impl.check.jsbz.newTechnology;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.dao.check.jsbz.newTechnology.FourTechnologyDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.jsbz.TechnologyApp;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.jsbz.newTechnology.FourTechnologyService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;

/**
 * 
 * FourTechnologyServiceImpl
 * 
 * 2017年8月11日 上午11:57:26
 * 
 * @version 1.0.0
 * 
 */
@Service
@Transactional
public class FourTechnologyServiceImpl implements FourTechnologyService{
	@Autowired
	private SystemManageService systemManageService;
	
	@Autowired
	public FourTechnologyDao fourTechnologyDao;
	
	@Autowired
	private CheckTableService checkTableService;
	
	public List<TechnologyApp> getFourTechnologyList(QueryFilter queryFilter){
		return fourTechnologyDao.find(queryFilter,"testYear", true);
	}
	/**
	 * 查询，根据查询条件获取结果
	 */
	@Override
	public Map<String, Object> getTechnologyApp(String startTime, String endTime, CheckTable checkTable, String tag)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		List<TechnologyApp> technologyAppList = new ArrayList<TechnologyApp>();
		List<Attachment> list = new ArrayList<Attachment>();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		double yearPart = Double.parseDouble((String)jsonObject.get("yearPart"));
		
		//取到所有文件
		list = systemManageService.getAllCheckAttachments(startTime, endTime, tag);
		//查询数据
		QueryFilter queryFilter = new QueryFilter();
		queryFilter.addFilter("testYear", startTime, MatchType.GE);
		queryFilter.addFilter("testYear", endTime, MatchType.LE);
		//检查记录
		double total = 0.0;				//得分
		double percentage = 0.0;		//完善度
		double tableFull = yearPart;
		tableFull = MathsUtils.formatDouble(tableFull, 2);         //满分
		
		technologyAppList = this.getFourTechnologyList(queryFilter);
		for (TechnologyApp technologyApp : technologyAppList) {
			if (technologyApp.getUseNum() >=2 && technologyApp.getUseSummary().equals("1")) {
				total += yearFull;
			}
		}
		if (total >= yearPart) {
			total = yearPart;
		}
		map.put("sum", total);
		//判断完善度
		percentage = MathsUtils.divDouble(total, tableFull, 2);
		percentage = MathsUtils.mulDouble(percentage, 100);
		map.put("percent", percentage);
		
		map.put("att", list);
		map.put("checkTable", checkTable);
		map.put("changeInves", technologyAppList);
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, true);
		map.put("score", checkTable.getTestFull());
		return map;
	}
}
